import { Tools } from "../../common/Tools";
import { EventManager } from "../../manager/EventManager";
import { EVENT_NAME_ENUM } from "../../base/EnumIndex";
import LevelPre_15 from "./LevelPre_15";
import LvItem from "../../LvItem";
import { UIManager } from "../../manager/UIManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_15_76 extends cc.Component {

    level15: LevelPre_15 = null


    activeNum: number = 0

    LoseStates = ["10", "2", "3", "4", "5", "6", "7", "8", "9"]
    wupin = ["手里工装完整", "扫把完整", "传单完整", "夹子完整", "安全帽完整"]
    bgtwupin = ["手里工装", "扫把手里", "传单手里", "夹子手里", "安全帽手里"]
    wupinIndex = 0
    start(): void {
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this)
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
    }

    playSpineAni(node: cc.Node, armatureName: string) {
        let spine = node.getComponent(sp.Skeleton);
        spine.setAnimation(0, armatureName, true)
    };

    finishTween(finishStr: string) {
        if (typeof finishStr != "string") {
            return;
        }

        console.log('111111111111111');
        if (finishStr.includes("updateLose")) {
            if (this.getIsLose()) {
                this.level15.updateIsWin();
            }
        }
        if (finishStr.includes("baogongtou")) {
            console.log('1212122121212212');
            this.gNa("被卖大门", false);
            this.switchWupin(this.bgtwupin[this.wupinIndex])
            cc.tween(this.gN("bgt"))
                .set({ opacity: 0 })
                .to(0.5, { opacity: 255 })
                .call(() => {
                    this.playSpineAni(this.gN("bgt"), "shuohua")
                    this.level15.sayNodeTween({ str: "你奶奶收了钱，快去干活吧。", audio: { resUrl: "你奶奶收了钱，快去干活吧。", value: 1 } });
                    this.gNa(this.wupin[this.wupinIndex], true);
                })
                .delay(3)
                .call(() => {
                    this.playSpineAni(this.gN("bgt"), "daiji")
                    this.wupinIndex++
                })
                .start();
        }
    }

    switchWupin(wupin) {
        this.gN("bgt").getComponent(LvItem).setBoneActive("传单手里", true);
        this.gN("bgt").getComponent(LvItem).setAttachment("传单手里", wupin);
    }

    gN(NodeName: string) {
        console.log(NodeName);

        if (!this.level15.getNodeForName(NodeName).getComponent(LvItem)) {
            this.level15.getNodeForName(NodeName).addComponent(LvItem)
        }
        return this.level15.getNodeForName(NodeName);
    }
    gNa(NodeName: string, active: boolean) {
        this.level15.getNodeForName(NodeName).active = active;
    }

    /** 是否过关 */
    getIsLose() {
        if (this.LoseStates.length != this.level15.finishStateArr.length || this.level15.finishStateArr.length <= 0) { return false; }
        for (let i = 0; i < this.level15.finishStateArr.length; i++) {
            if (this.LoseStates.indexOf(this.level15.finishStateArr[i]) < 0) {
                return false;
            }
        }
        return true;
    };

    onDisable() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this)
    }
}
